package com.example.helloworld.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

public class RoundImage extends Drawable {

	private static final int DIMENSION = 100;
	private int mRadius;
	private RectF mRect;
	private Paint mPaint;
	
		private int dpToPixel(Context context, int dp) {
			return (int)TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
		}
		
	public RoundImage(Context context, Bitmap bitmap, int radius) {
		mRadius = radius;
		
		int w = dpToPixel(context, 150);
		int h = dpToPixel(context, 120);
		
		Bitmap target = Bitmap.createScaledBitmap(bitmap, w, h, true);
		BitmapShader shader = new BitmapShader(target, TileMode.CLAMP, TileMode.CLAMP);
		
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setShader(shader);
		
		mRect = new RectF(0f, 0f, DIMENSION, DIMENSION);
	}
	
	public void moveTo(int dx, int dy) {
		mRect.offset(dx, dy);
		invalidateSelf();
	}
	
	@Override
	public void draw(Canvas canvas) {
		canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint);
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
	}
	
}
